package com.chinacoal.hr.levelimpl.dao;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.chinacoal.hr.levelapi.entity.AttendanceSummary;
import com.chinacoal.hr.levelimpl.mapper.AttendanceSummaryMapper;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * @author: YunTao.Li
 * @create: 2019-11-26 14:49
 * @description: 考勤汇总DAO
 **/
@Component
public class AttendanceSummaryDao {

    @Resource
    private AttendanceSummaryMapper attendanceSummaryMapper;

    /**
     * 考勤信息汇总查询带分页
     *
     * @param condition :
     * @return : com.baomidou.mybatisplus.core.metadata.IPage<com.chinacoal.hr.levelimpl.entity.AttendanceSummary>
     * @author : YunTao.Li
     * @date : 2019/11/26 2019/11/26
     */
    public IPage<AttendanceSummary> getAttendanceInfoList(Map<String, Object> condition) {
        Long currentPage = (Long) condition.get("page");
        Long currentPageSize = (Long) condition.get("limit");

        String empCode = condition.get("empCode") != null ? condition.get("empCode").toString() : "";
        String orgCode = condition.get("orgCode") != null ? condition.get("orgCode").toString() : "";
        String deptName = condition.get("deptName") != null ? condition.get("deptName").toString() : "";
        String yrmonth = condition.get("yrmonth") != null ? condition.get("yrmonth").toString() : "";

        IPage<AttendanceSummary> pageObject = new Page(currentPage, currentPageSize);

        // 开始加入查询条件，为空的值将不会被拼接到查询条件中
        QueryWrapper queryWrapper = new QueryWrapper();

        if (StringUtils.isNotBlank(empCode)) {
            queryWrapper.eq("EMP_CODE", empCode);
        }

        if (StringUtils.isNotBlank(orgCode)) {
            queryWrapper.eq("ORG_CODE", orgCode);
        }
        if (StringUtils.isNotBlank(deptName)) {
            queryWrapper.like("DEPT_NAME", deptName);
        }
        if (StringUtils.isNotBlank(yrmonth)) {
            queryWrapper.like("PERIOD_CODE", yrmonth);
        }

        queryWrapper.orderByDesc("PERIOD_CODE");
        queryWrapper.orderByDesc("ORG_CODE");
        queryWrapper.orderByDesc("DEPT_CODE");
        queryWrapper.orderByDesc("KQ_NO");


        IPage<AttendanceSummary> attendanceInfoIPage = attendanceSummaryMapper.selectPage(pageObject, queryWrapper);

        return attendanceInfoIPage;
    }

    public void insertAttendanceSummaryRecord(AttendanceSummary record) {
        attendanceSummaryMapper.insert(record);
    }

    public void insertBatchAttendanceSummaryRecords(List<AttendanceSummary> records) {
        for (AttendanceSummary record : records) {
            this.insertAttendanceSummaryRecord(record);
        }
    }

    public void updateAttendanceSummaryRecord(AttendanceSummary record) {
        attendanceSummaryMapper.updateById(record);
    }

    /**
     * 根据条件删除考勤汇总数据
     *
     * @param condition :
     * @return : void
     * @author : YunTao.Li
     * @date : 2020/7/6 2020/7/6
     */
    public void deleteAttendanceSummaryRecordByCondition(Map<String, Object> condition) {
        int conditionCount = 0;
        String yrmonth = condition.get("yrmonth") != null ? condition.get("yrmonth").toString() : "";

        QueryWrapper queryWrapper = new QueryWrapper();

        if (StringUtils.isNotBlank(yrmonth)) {
            queryWrapper.eq("PERIOD_CODE", yrmonth);
            conditionCount++;
        }

        if (conditionCount > 0) {
            this.attendanceSummaryMapper.delete(queryWrapper);
        }

    }
}
